<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Cache,Event;
class UkController extends Controller
{

    public function index($type = 0,$id = 0,request $request)
    {
        if($type == 1){
            if($id == 0){
                $map = array();
            }else{
                $map['city'] = $id;
            }
        } elseif($type == 2){
            $map['area'] = $id;
        }elseif($type == 3){
            $map['org_id'] = $id;
        }else{
            $map = array();
        }

        if($request->unit_name){
            $map[] = array('unit_name','like','%'.$request->unit_name.'%');
        }

        $res = DB::table('usbkey')->where($map)->where('type','D')->orderby('id')->paginate(10);
        $count = DB::table('usbkey')->where($map)->where('type','D')->count();
        return view('admin.uk.index', ['res' => $res,'count'=>$count]);
    }

    public function index2()
    {
        $res = DB::table('usbkey')->where('type','P')->orderby('id')->paginate(10);
        $count = DB::table('usbkey')->where('type','P')->count();
        return view('admin.uk.index2', ['res' => $res,'count'=>$count]);
    }

    public function add(Request $request)
    {
        if($request->isMethod('post')){
            if(!$_POST['key_id']){
                echo "<script>alert('请插入usbkey!');window.history.go(-1);</script>";
                exit();
            }
            $re = db::table('usbkey')->where('key_id',$_POST['key_id'])->first();
            if($re){
                echo "<script>alert('该UKEY已经激活，请勿重复操作!');window.history.go(-1);</script>";
                exit();
            }
            if(!$_POST['city']){
                echo "<script>alert('请选择市!');window.history.go(-1);</script>";
                exit();
            }
            if(!$_POST['area']){
                echo "<script>alert('请选择区!');window.history.go(-1);</script>";
                exit();
            }
            $city_bianhao = getValue($_POST['city'],'area' ,'id' ,'bianhao' );
            $area_bianhao = getValue($_POST['area'],'area' ,'id' ,'bianhao' );
            $map['city'] = $city_bianhao;
            $map['area'] = $area_bianhao;
            $num = db::table('usbkey_num')->where($map)->value('num');
            $bianhao = str_pad($num+100001, 6, '0', STR_PAD_LEFT);

            $data['username'] = 'D'.$city_bianhao.$area_bianhao.$bianhao;
            $data['key_id'] = $_POST['key_id'];
            $data['type'] = 'D';
            $data['city'] = $_POST['city'];
            $data['area'] = $_POST['area'];
            $data['created_at'] = date('Y-m-d H:i:s');
            $id = db::table('usbkey')->insertGetId($data);

            //添加成功则数量加1
            if($id){
                DB::table('usbkey_num')->where($map)->increment('num','1');
                echo "<script>alert('激活成功，用户名为".$data['username']."!');window.location.href='".$_SERVER["HTTP_REFERER"]."';</script>";
                exit();
            }
        }
        $city = db::table('area')->where('cid','0')->get();
        return view('admin.uk.add',['city'=>$city]);
    }

    public function add2(Request $request)
    {
        if($request->isMethod('post')){
            if(!$_POST['key_id']){
                echo "<script>alert('请插入usbkey!');window.history.go(-1);</script>";
                exit();
            }
            $re = db::table('usbkey')->where('key_id',$_POST['key_id'])->first();
            if($re){
                echo "<script>alert('该UKEY已经激活，请勿重复操作!');window.history.go(-1);</script>";
                exit();
            }

            if(!$_POST['dengji']){
                if(!$_POST['city']){
                    echo "<script>alert('请选择警员级别!');window.history.go(-1);</script>";
                    exit();
                }
            }
            if($_POST['dengji'] == 2){
                if(!$_POST['city']){
                    echo "<script>alert('请选择市!');window.history.go(-1);</script>";
                    exit();
                }
                $data['city'] = $_POST['city'];
            }
            if($_POST['dengji'] == 3){
                if(!$_POST['city'] || !$_POST['area']){
                    echo "<script>alert('请选择市、区!');window.history.go(-1);</script>";
                    exit();
                }
                $data['city'] = $_POST['city'];
                $data['area'] = $_POST['area'];
            }
            if($_POST['dengji'] == 4){
                if(!$_POST['city'] || !$_POST['area'] || !$_POST['org_id']){
                    echo "<script>alert('请选择市、区、派出所!');window.history.go(-1);</script>";
                    exit();
                }
                $data['city'] = $_POST['city'];
                $data['area'] = $_POST['area'];
                $data['org_id'] = $_POST['org_id'];
            }

            $num = db::table('usbkey_num2')->where('id','1')->value('num');
            $bianhao = str_pad($num+100001, 6, '0', STR_PAD_LEFT);
            $data['username'] = 'P'.$bianhao;
            $data['key_id'] = $_POST['key_id'];
            $data['dengji'] =$_POST['dengji'];
            $data['type'] = 'P';
            $data['created_at'] = date('Y-m-d H:i:s');
            $id = db::table('usbkey')->insertGetId($data);

            $permission_id = '';
            if($_POST['dengji'] == 1){
                $data2['dengji'] = $_POST['dengji'];
                $data2['role_id'] = '18';//总队角色id
            }
            if($_POST['dengji'] == 2){
                //获取市下面的区
                $qu = db::table('area')->where('cid','=',$_POST['city'])->pluck('id');
                foreach ($qu as $k2){
                    //获取区下面的派出所
                    $pcs = db::table('area')->where('cid','=',$k2)->pluck('id');
                    foreach ($pcs as $k3){
                        $permission_id .= $k3 . ',';
                    }
                    $permission_id .= $k2 . ',';
                }
                $permission_id .= $_POST['city'] . ',';
                $data2['city'] = $_POST['city'];
                $data2['area_id'] = substr($permission_id, 0, -1);
                $data2['dengji'] = $_POST['dengji'];
                $data2['role_id'] = '24';//支队角色id
            }
            if($_POST['dengji'] == 3){
                //获取区上面的市
                $shi = db::table('area')->where('id','=',$_POST['area'])->value('cid');
                $permission_id .= $shi . ',';
                //获取区下面的派出所
                $pcs = db::table('area')->where('cid','=',$_POST['area'])->pluck('id');
                foreach ($pcs as $k3){
                    $permission_id .= $k3 . ',';
                }
                $permission_id .= $_POST['area'] . ',';
                $data2['city'] = $shi;
                $data2['area'] = $_POST['area'];
                $data2['area_id'] = substr($permission_id, 0, -1);
                $data2['dengji'] = $_POST['dengji'];
                $data2['role_id'] = '25';//大队角色id
            }
            if($_POST['dengji'] == 4){
                //获取派出所上面的区
                $qu = db::table('area')->where('id','=',$_POST['org_id'])->value('cid');
                $permission_id .= $qu . ',';
                //获取区上面的市
                $shi = db::table('area')->where('id','=',$qu)->value('cid');
                $permission_id .= $shi . ',';
                $permission_id .= $_POST['org_id'] . ',';
                $data2['city'] = $shi;
                $data2['area'] = $qu;
                $data2['org_id'] = $_POST['org_id'];
                $data2['area_id'] = substr($permission_id, 0, -1);
                $data2['dengji'] = $_POST['dengji'];
                $data2['role_id'] = '15';//派出所角色id
            }
            $data2['key_id'] = $_POST['key_id'];
            $data2['email'] = $data['username'];
            $data2['created_at'] = date('Y-m-d H:i:s',time());


            $data3['user_id'] = db::table('admin_users')->insertGetId($data2);
            if($data3['user_id']){
                db::table('usbkey')->where('id',$id)->update($data3);
                if($id){
                    DB::table('usbkey_num2')->where('id','1')->increment('num','1');
                    echo "<script>alert('激活成功!');window.location.href='".$_SERVER["HTTP_REFERER"]."';</script>";
                    exit();
                }
            }else{
                echo "<script>alert('激活失败!');window.location.href='".$_SERVER["HTTP_REFERER"]."';</script>";
                exit();
            }
        }
        $city = db::table('area')->where('cid','0')->get();
        return view('admin.uk.add2',['city'=>$city]);
    }

    public function edit($id,Request $request)
    {
        $re = DB::table('area')->where('id',$id)->first();
        if($request->isMethod('post')){
            $obj = DB::table('area');
            $data = Input::except('_token');
            $data['updated_at'] = date('Y-m-d H:i:s',time());
            $obj->where('id',$id)->update($data);
            $cid = $obj->where('id',$id)->value('cid');
            return redirect('admin/area/index/'.$cid);
        }
        return view('admin.area.edit', ['re' => $re]);
    }

    public function del($id)
    {
        $child = db::table('area')->where('cid', $id)->first();
        if ($child) {
            echo "<script>alert('该权限下有子权限!');window.history.go(-1);</script>";
            exit();
        }else{
            db::table('area')->where('id',$id)->delete();
            return redirect()->back();
        }
    }
}
